package com.course.servlet;

import com.course.entity.Teacher;
import com.course.service.TeacherService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/laoshi_edit_teacher")
public class LaoshiEditTeacherServlet extends HttpServlet {
    private final TeacherService teacherService = new TeacherService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String idStr = request.getParameter("id");
        if (idStr != null && !idStr.isEmpty()) {
            try {
                int id = Integer.parseInt(idStr);
                Teacher teacher = teacherService.findById(id);
                if (teacher != null) {
                    request.setAttribute("teacher", teacher);
                    request.getRequestDispatcher("/WEB-INF/jsp/laoshi_edit_teacher.jsp").forward(request, response);
                } else {
                    response.sendRedirect(request.getContextPath() + "/guanliyuan_teacher_manage"); // Teacher not found
                }
            } catch (NumberFormatException e) {
                response.sendRedirect(request.getContextPath() + "/guanliyuan_teacher_manage"); // Invalid ID
            }
        } else {
            response.sendRedirect(request.getContextPath() + "/guanliyuan_teacher_manage"); // No ID parameter
        }
    }

        @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

            int i = Integer.parseInt(request.getParameter("id"));
            String teacherNumber = request.getParameter("teacherNumber");
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String name = request.getParameter("name");
            String gender = request.getParameter("gender");
            String department = request.getParameter("department");
            String email = request.getParameter("email");
            String phone = request.getParameter("phone");


            Teacher teacher = new Teacher();
            teacher.setId(i);
            teacher.setTeacherNumber(teacherNumber);
            teacher.setUsername(username);
            teacher.setPassword(password);
            teacher.setName(name);
            teacher.setGender(gender);
            teacher.setDepartment(department);
            teacher.setEmail(email);
            teacher.setPhone(phone);
            teacherService.updateTeacher(teacher);
            response.sendRedirect(request.getContextPath() + "/guanliyuan_teacher_manage.jsp");


        }

















































} 